<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>新增用户</title>
    <link href="${basePath }/static/plugins/layui/css/layui.css" rel="stylesheet"/>
    <link href="${basePath }/static/plugins/filepond/filepond.css" rel="stylesheet"/>
    <link href="${basePath}/static/plugins/email-auto-complete/emailAutoComplete.css" rel="stylesheet">
</head>
<body>
<form id="user-form" class="layui-form" action="javascript:;" style="margin: 24px 48px 0 0;">
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <div class="parentCls">
                <input type="text" name="email" placeholder="请输入邮箱" class="layui-input inputElem">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" placeholder="请输入用户名" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">昵称</label>
        <div class="layui-input-block">
            <input type="text" name="nickname" placeholder="请输入昵称" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">锁定</label>
        <div class="layui-input-block">
            <input type="checkbox" name="lock" lay-skin="switch">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">头像</label>
        <div class="layui-input-block">
            <input type="file"
                   class="filepond"
                   name="avatar"
                   multiple
                   data-max-file-size="10MB"
                   data-max-files="10"
                   accept="image/png, image/jpeg, image/gif"/>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="user-form">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="${basePath }/static/plugins/layui/layui.js"></script>
<script src="${basePath }/static/plugins/jq/jquery-2.2.4.min.js"></script>
<script src="${basePath }/static/plugins/filepond/filepond.js"></script>
<script src="${basePath }/static/build/js/common.js"></script>
<script>
    layui.use(['form', 'layer', 'laydate'], function() {
        var form = layui.form,
            laydate = layui.laydate,
            layer = layui.layer;

        // Select the file input and use
        // create() to turn it into a pond
        FilePond.create(
            document.querySelector('input[type="file"]'),
            {
                labelIdle: '拖放文件或 <span class="filepond--label-action">点击浏览</span>',
                imagePreviewHeight: 170,
                imageCropAspectRatio: '1:1',
                imageResizeTargetWidth: 200,
                imageResizeTargetHeight: 200
            }
        );
        FilePond.setOptions({
            server: 'api/'
        });
        FilePond.registerPlugin(
            FilePondPluginImagePreview,
            FilePondPluginImageExifOrientation,
            FilePondPluginFileValidateSize
        );


        form.render();
        form.on('submit(user-form)', function(data){
            var reqData = data.field;
            console.log(data);
            $.ajax({
                url: '${basePath}/admin/user/insert',
                type: 'post',
                dataType: 'json',
                data: reqData,
                success: function(resp){
                    if(resp.code==10000){
                        layer.msg('新增成功',{
                            time: 1000
                        }, function(){
                            $('#user-form button[type="reset"]').trigger('click');
                        })
                    }
                }
            })
            return false;
        });
    });
</script>
</body>
</html>
